package com.xyhy.core.file.oss.aliyun;

import cn.hutool.json.JSONUtil;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Builder;
import lombok.Getter;
import lombok.Setter;
import lombok.experimental.Accessors;
import com.xyhy.core.file.oss.IOssParams;

import java.nio.charset.StandardCharsets;
import java.util.Base64;
import java.util.Optional;

@Getter
@Setter
@Accessors(chain = true)
@ApiModel("阿里文件上传参数")
public class AliUploadParams implements IOssParams {
  @ApiModelProperty("ali access key")
  private String accessKeyId;

  @ApiModelProperty("签名")
  private String signature;

  @ApiModelProperty("ali Bucket")
  private String bucket;

  @ApiModelProperty("站点")
  private String endpoint;

  @ApiModelProperty("上传策略")
  private String policy;

  @ApiModelProperty("回调体")
  private Callback callback;

  public String getCallback() {
    return Optional.ofNullable(callback).map(x -> Base64.getEncoder().encodeToString(JSONUtil.toJsonStr(x).getBytes(StandardCharsets.UTF_8))).orElse(null);
  }

  @Getter
  @Setter
  @Builder
  @ApiModel("阿里文件上传回调参数")
  public static class Callback {
    @ApiModelProperty("回调 url")
    private String callbackUrl;

    @ApiModelProperty("回调")
    private String callbackHost;

    @ApiModelProperty("回调消息体类型")
    private String callbackBodyType;

    @ApiModelProperty("回调消息体")
    private String callbackBody;

  }


}
